package com.xy.p2p.action.front;



import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.web.CookieUtils;
import com.xy.core.util.BeanUtil;
import com.xy.core.web.WebErrors;
import com.xy.p2p.model.Tfinanceinfo;
import com.xy.p2p.model.Tmember;
import com.xy.p2p.model.Ttenderrecord;
import com.xy.p2p.model.TtenderrecordForTransfer;
import com.xy.p2p.model.Ttransferinfo;
import com.xy.p2p.service.TfinanceinfoService;
import com.xy.p2p.service.TmemberService;
import com.xy.p2p.service.TtenderrecordService;



@Controller
public class TtenderrecordAction{
	private static final Logger log = LoggerFactory.getLogger(TtenderrecordAction.class);
	public TtenderrecordAction() {
	}
	//余额贷(标的类型)
	private static final int TFINANCEINFO_TARGETTYPE_YED=8;
	
	@RequestMapping("/member/o_ttenderrecordTender.jspx")
	public String tender(HttpServletRequest request, ModelMap model,Ttenderrecord ttenderrecord) {
		setDict(model);
		Tfinanceinfo tfinanceinfo = tfinanceinfoService.get(ttenderrecord.getTfinanceinfo().getFiid());
		if(TFINANCEINFO_TARGETTYPE_YED==tfinanceinfo.getTargettype()){
			this.tenderYED(request, model, ttenderrecord);
		}
		ttenderrecord.setTmember((Tmember)request.getSession().getAttribute(TmemberAction.AUTH_KEY));
		String msg = ttenderrecordService.tender(ttenderrecord);
		//操作成功
		if(null == msg)
			return "redirect:/member/v_ttenderrecordList.jspx";
		//
		if(msg.indexOf("余额不足")!=-1)
			return "forward:/member/rechargeInit.jspx";//跳转到充值页面
		model.addAttribute("EMSG", msg);
		return "forward:/member/v_tfinanceinfoList1.jspx";
	}
	
	@RequestMapping("/member/o_ttenderrecordTenderYED.jspx")
	public String tenderYED(HttpServletRequest request, ModelMap model,Ttenderrecord ttenderrecord) {
		setDict(model);
		Tfinanceinfo tfinanceinfo = tfinanceinfoService.get(ttenderrecord.getTfinanceinfo().getFiid());
		if(TFINANCEINFO_TARGETTYPE_YED!=tfinanceinfo.getTargettype()){
			this.tender(request, model, ttenderrecord);
		}
		ttenderrecord.setTmember((Tmember)request.getSession().getAttribute(TmemberAction.AUTH_KEY));
		String msg = ttenderrecordService.tenderYED(ttenderrecord);
		//操作成功
		if(null == msg)
			return "redirect:/member/v_ttenderrecordList.jspx";
		//
		if(msg.indexOf("余额不足")!=-1)
			return "forward:/member/rechargeInit.jspx";//跳转到充值页面
		model.addAttribute("EMSG", msg);
		return "forward:/member/v_tfinanceinfoYEDList.jspx";
	}

	@RequestMapping("/member/v_ttenderrecordList.jspx")
	public String List(HttpServletRequest request, ModelMap model,Integer pageNo) {
		setDict(model);
		Tmember tmember = (Tmember)request.getSession().getAttribute(TmemberAction.AUTH_KEY);
		Ttenderrecord ttenderrecord = new Ttenderrecord();
		ttenderrecord.setTmember(tmember);
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = ttenderrecordService.getTtenderrecordByPage(toMap(ttenderrecord),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
//		model.addAttribute("list", ttenderrecordService.getTtenderrecord(toMap(ttenderrecord)));
		//用于导航栏
		model.addAttribute("dis", 10);
		return "/front/member/tenderList.html";
	}
	
	
	@RequestMapping("/member/v_ttenderrecordList1.jspx")
	public String List1(HttpServletRequest request, ModelMap model) {
		setDict(model);
		Tmember tmember = (Tmember)request.getSession().getAttribute(TmemberAction.AUTH_KEY);
		Ttenderrecord ttenderrecord = new Ttenderrecord();
		ttenderrecord.setTmember(tmember);
		List<Ttenderrecord> trList = ttenderrecordService.getTtenderrecord(toMap(ttenderrecord));
		List<TtenderrecordForTransfer> ttList = new ArrayList<TtenderrecordForTransfer>();
		for(Ttenderrecord t : trList){
//			TtenderrecordForTransfer tt = (TtenderrecordForTransfer)t;
			TtenderrecordForTransfer tt = new TtenderrecordForTransfer(t);
			ttList.add(tt);
		}
		model.addAttribute("list", ttList);
		return "/front/member/transferinfoList.html";
	}
	


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param ttenderrecord
	 * @return
	 */	private Map<String,Object> toMap(Ttenderrecord ttenderrecord){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(ttenderrecord.getTrid()!=null&&!ttenderrecord.getTrid().equals("")){
			map.put("trid", ttenderrecord.getTrid());
		}
		if(ttenderrecord.getTfinanceinfo()!=null){
			map.put("Tfinanceinfo", ttenderrecord.getTfinanceinfo());
		}
		if(ttenderrecord.getTmember()!=null){
			map.put("Tmember", ttenderrecord.getTmember());
		}
		if(ttenderrecord.getTdmoney()!=null&&!ttenderrecord.getTdmoney().equals("")){
			map.put("tdmoney", ttenderrecord.getTdmoney());
		}
		if(ttenderrecord.getTddatetime()!=null&&!ttenderrecord.getTddatetime().equals("")){
			map.put("tddatetime", ttenderrecord.getTddatetime());
		}
		if(ttenderrecord.getTdreason()!=null&&!ttenderrecord.getTdreason().equals("")){
			map.put("tdreason", ttenderrecord.getTdreason());
		}
		if(ttenderrecord.getFinancepa()!=null&&!ttenderrecord.getFinancepa().equals("")){
			map.put("financepa", ttenderrecord.getFinancepa());
		}
		if(ttenderrecord.getMibmoney()!=null&&!ttenderrecord.getMibmoney().equals("")){
			map.put("mibmoney", ttenderrecord.getMibmoney());
		}
		if(ttenderrecord.getIstransfer()!=null&&!ttenderrecord.getIstransfer().equals("")){
			map.put("istransfer", ttenderrecord.getIstransfer());
		}
		if(ttenderrecord.getTsfmoney()!=null&&!ttenderrecord.getTsfmoney().equals("")){
			map.put("tsfmoney", ttenderrecord.getTsfmoney());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
      model.addAttribute("istransferMap", DictUtils.getDictByType(DictUtils.TTENDERRECORD_ISTRANSFER));
      model.addAttribute("targettypeMap", DictUtils.getDictByType(DictUtils.TFINANCEINFO_TARGETTYPE));
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Ttenderrecord ttenderrecord, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TtenderrecordService ttenderrecordService;   
	@Resource
	private TfinanceinfoService tfinanceinfoService;
	@Resource
	private TmemberService tmemberService;
}